from math import inf


class Solution:
    def numSquares(self, n: int) -> int:
        dp = [0] * (n + 1)
        for i in range(1, n + 1):
            minVal = inf
            j = 1
            while j * j <= i:
                minVal = min(minVal, dp[i - j * j])
                j += 1 
            dp[i] = minVal + 1
        return dp[n]